//引入css文件
require("../css/login.less")
require("../fonts/iconfont.css")


//获取元素
let usname = document.querySelector("#usname")
let pwd = document.querySelector("#pwd")
let loginbtn = document.querySelector("#loginbtn")


let Telflag = false
var reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
usname.addEventListener("blur", function () {
    let bool = reg_tel.test(usname.value)
    if (!bool) {
        console.log("无效的手机号");
        Telflag = false
        this.classList.add("change")
    } else {
        console.log("手机号验证通过");
        Telflag = true
    }
})


//密码框验证
var reg_pwd = /^[0-9]{6,18}$/;
let Pwdflag = true
pwd.addEventListener("blur", function () {
    let bool = reg_pwd.test(pwd.value)
    if (!bool) {
        console.log("无效的密码");
        Pwdflag = false
    } else {
        console.log("验证通过");
        Pwdflag = true
    }
})

//表单提交事件
loginbtn.addEventListener("click", function () {
    // alert(1)
    let bool = Pwdflag && Telflag
    if (bool) {
        console.log("验证通过");
        window.$http.post("/users/login", {
            account: usname.value,
            password: pwd.value
        }, function (res) {
            console.log(res);
            if (res.status == 0) {
                window.localStorage.setItem("token", res.data.token)
                window.localStorage.setItem("user", JSON.stringify(res.data.user))
                window.$utils.tick(res.status, res.msg, function () {//这里传了回调函数,代表fn为true,即执行函数
                    window.location.href = "./index.html"
                })
            } else {
                window.$utils.tick(res.status, res.msg) //这里没有传回调函数,相当于fn为false
            }
        })
    }
})